<template>
  <!-- 待办任务 -->
  <div style="position: relative;">
    <el-form label-width="100px" size="mini" label-position="right" @submit.native.prevent>
      <filter-box :item-width="350">
        <el-button slot="operator" type="primary" :plain="true" size="mini" @click="refreshMyTask(true)">查询</el-button>
      </filter-box>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table ref="flowEntry" :data="formMyTask.taskWidget.dataList" size="mini" @sort-change="formMyTask.taskWidget.onSortChange"
          header-cell-class-name="table-header-gray">
          <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" :index="formMyTask.taskWidget.getTableIndex" />
          <el-table-column label="流程名称" prop="processDefinitionName" />
          <el-table-column label="当前任务" prop="taskName" />
          <el-table-column label="任务发起人" prop="processInstanceInitiatorName" />
          <el-table-column label="任务发起时间" prop="processInstanceStartTime" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button class="table-btn primary" size="mini" type="text" @click="onSubmit(scope.row)">办理</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            :total="formMyTask.taskWidget.totalCount"
            :current-page="formMyTask.taskWidget.currentPage"
            :page-size="formMyTask.taskWidget.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formMyTask.taskWidget.onCurrentPageChange"
            @size-change="formMyTask.taskWidget.onPageSizeChange">
          </el-pagination>
        </el-row>
      </el-col>
    </el-row>
    <div class="annotation" v-if="this.setPlanbox">
      <Plansadd ref="setPlan" @getPlans="getPlans" v-model="this.setPlanbox"></Plansadd>
    </div>
   
  </div>
</template>

<script>
import '@/staticDict/flowStaticDict.js';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { FlowDictionaryController, FlowOperationController } from '@/api/flowController.js';

import { getObjectFromSessionStorage } from '@/utils';
import ProjectTasksAdd from '../../Projects/ProjectsTasksAdd';
import { Administration, Contracts } from '@/api/projectAdministration.js';
import ContractTasksAdd from '../../Projects/ContractsTasksAdd';
import CommercialTaskAdd from '../../Commercial/CommercialTaskAdd';
import { BusinessOpportunities } from '@/api/commercial.js';
import Oppoadd from '../../Commercial/Oppoadd';
import Plansadd from '../../Projects/Projectsinfo/Plansadd';
export default {
  name: 'formMyTask',
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  components: { Plansadd},
  data () {
    return {
      setPlanbox: false,
      formMyTask: {
        taskWidget: new TableWidget(this.loadTaskData, this.loadTaskDataVerify, true, false, 'entryId', 1),
        isInit: false
      },
      totalCount: '',
      flowTypelist: [
        {type: 1, name: '项目', pathurl: '/ProjectsExamine', method: this.showProject},
        {type: 2, name: '合同', pathurl: '/ContractsExamine', method: this.showContract},
        {type: 3, name: '商机任务', pathurl: '/CommercialExamine', method: this.showOppoTask},
        {type: 4, name: '项目任务', pathurl: '/ProjectsTasksExamine', method: this.showProjectTask},
        {type: 5, name: '合同任务', pathurl: '/ContractsTasksExamine', method: this.showContractTask},
        {type: 6, name: '项目计划变更', pathurl: '/PlanChange', method: this.showProjectPlanChange},
        {type: 7, name: '商机', pathurl: '/OpportunitiesExamine', method: this.showOpportunity}
      ]
    }
  },
 
  methods: {
    loadTaskData (params) {
      if (params == null) params = {};
      params = {
        ...params
      }
      return new Promise((resolve, reject) => {
        FlowOperationController.listRuntimeTask(this, params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          });
          sessionStorage.setItem('agendaList', JSON.stringify(res.data.totalCount))
        }).catch(e => {
          reject(e);
        });
      });
    },
    loadTaskDataVerify () {
      return true;
    },
    showProject (projectId) {
      this.$router.push({path: '/Projectsadd', query: {projectId: projectId}})
    },
    showContract (contractId) {
      this.$router.push({path: '/Winasingle', query: {contractId: contractId}})
    },
    showProjectPlanChange (changeId) {
      let params = {
        changeId: changeId
      };
      return new Promise((resolve, reject) => {
        Administration.planchange(this, params).then(res => {
          this.setPlanbox = true
          this.$nextTick(() => {
            this.$refs.setPlan.info(res.data.projectId, changeId)
          })
        }).catch(e => {
          reject(e);
        });
      });
    },
    getPlans (data) {
      let params = {
        saveDto: {
          projectId: this.$route.query.projectId,
          ...data
        }
      };
      return new Promise((resolve, reject) => {
        Administration.plansave(this, params).then(res => {
          this.$message({
            showClose: true,
            message: '操作成功！',
            type: 'success'
          });
          this.refreshMyTask(true)
        }).catch(e => {
          reject(e);
        });
      });
    },
    showOpportunity (opportunityId) {
      this.$dialog.show('计划变更', Oppoadd, {
        area: '800px'
      }, {
        oppoId: opportunityId
      }).then(res => {
        this.refreshMyTask(true)
      }).catch(e => {
      });
    },
    showOppoTask (taskId) {
      let params = {
        id: taskId
      };
      return new Promise((resolve, reject) => {
        BusinessOpportunities.Taskdetail(this, params).then(res => {
          this.$dialog.show('商机任务', CommercialTaskAdd, {
            area: '800px'
          }, {
            tasksId: taskId,
            businessType: res.data.businessType,
            oppoId: res.data.opportunityId
          }).then(res => {
            this.refreshMyTask(true)
          }).catch(e => {
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    showContractTask (taskId) {
      let params = {
        id: taskId
      };
      return new Promise((resolve, reject) => {
        Contracts.taskview(this, params).then(res => {
          let paramTask = {
            tasksId: taskId,
            businessType: res.data.businessType,
            contractId: res.data.contractId
          };
          this.$dialog.show('合同计划', ContractTasksAdd, {
            area: '800px'
          }, paramTask).then(res => {
            this.refreshMyTask(true)
          }).catch(e => {});
        }).catch(e => {
          reject(e);
        });
      });
    },
    showProjectTask (taskId) {
      let params = {
        id: taskId
      };
      return new Promise((resolve, reject) => {
        Administration.taskview(this, params).then(res => {
          let paramTask = {
            tasksId: taskId,
            businessType: res.data.businessType,
            projectId: res.data.projectId
          };
          this.$dialog.show('项目任务', ProjectTasksAdd, {
            area: '800px'
          }, paramTask).then(() => {
            this.refreshMyTask(true)
          }).catch(e => {
            reject(e);
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    onSubmit (row) {
      let flowType = this.flowTypelist.find((item, index) => {
        return row.businessFlowType === item.type
      })
      if (flowType) {
        let userInfo = getObjectFromSessionStorage('userInfo')
        if (userInfo.loginName === row.processInstanceInitiator && row.taskName.indexOf('申请人') > -1) {
          flowType.method(row.businessKey)
        } else {
          this.$router.push({
            path: flowType.pathurl,
            query: {
              businessKey: row.businessKey,
              taskId: row.taskId,
              processInstanceId: row.processInstanceId
              
            }
          })
        }
      } else {
        let params = {
          processInstanceId: row.processInstanceId,
          processDefinitionId: row.processDefinitionId,
          taskId: row.taskId
        }

        FlowOperationController.viewRuntimeTaskInfo(this, params).then(res => {
          if (res.data) {
            this.$router.push({
              name: res.data.routerName || 'handlerFlowTask',
              query: {
                isRuntime: true,
                taskId: row.taskId,
                processDefinitionKey: row.processDefinitionKey,
                processInstanceId: row.processInstanceId,
                processDefinitionId: row.processDefinitionId,
                formId: res.data.formId,
                routerName: res.data.routerName,
                readOnly: res.data.readOnly,
                taskName: row.taskName,
                flowEntryName: row.processDefinitionName,
                processInstanceInitiator: row.processInstanceInitiator,
                processInstanceInitiatorName: row.processInstanceInitiatorName,
                // 过滤掉加签操作，加签只有在已完成任务里可以操作
                operationList: (res.data.operationList || []).filter(item => item.type !== this.SysFlowTaskOperationType.CO_SIGN),
                variableList: res.data.variableList
              }
            });
          }
        }).catch(e => {});
      }
    },
    refreshMyTask (reloadData = false) {
      if (reloadData) {
        this.formMyTask.taskWidget.refreshTable(true, 1);
      } else {
        this.formMyTask.taskWidget.refreshTable();
      }
    },
    onResume () {
      this.refreshMyTask();
    },
    formInit () {
      this.refreshMyTask();
    }
  },
  mounted () {
    // 初始化页面数据
    this.formInit();
  }
}
</script>

<style>
</style>
